//
//  HWRankListProtocol.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/29.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit

class HWRankListProtocol: NSObject {
    var type = HWMineNavType.popularityRank
    var rankListModels:[HWRankListModel]?
    var consumeRankModels:[HWConsumeRankModel]?
}

extension HWRankListProtocol:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        if section == 0 {
            return UIEdgeInsets(top: 0, left: 22*widthScale, bottom: 0, right: 22*widthScale)
        }else {
            return UIEdgeInsets.zero
        }
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let  header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HWHomeHeader", for: indexPath) as! HWHomeHeader
        if type == .popularityRank {
            header.titleLabel.text = "游戏排行榜"
            header.imageView.image = UIImage(named: "gameRank")
        }else if type == .consumeRank {
            header.titleLabel.text = "充值排行榜"
            header.imageView.image = UIImage(named: "consumeRank")
        }
            
            
        return header
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        if section == 0 {
            return CGSize(width: screenWidth, height: 66*widthScale)
        }else {
            return CGSize.zero
        }
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 0 {
            return CGSize(width: (screenWidth-44*widthScale)/3, height: 162*widthScale)
        }else {
            return CGSize(width: (screenWidth - 40*widthScale), height: 46*widthScale)
        }
        
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0 {
            if type == .popularityRank {
                if (rankListModels?.count ?? 0) > 2 {
                    return 3
                }else {
                    return rankListModels?.count ?? 0
                }
            }else if type == .consumeRank {
                if (consumeRankModels?.count ?? 0) > 2 {
                    return 3
                }else {
                    return consumeRankModels?.count ?? 0
                }
            }else {
                return 0
            }
            
            
        }else {
            if type == .popularityRank {
                if (rankListModels?.count ?? 0) > 2{
                    return (rankListModels?.count ?? 0) - 3 + 1
                }else{
                    return 0
                }
            }else if type == .consumeRank {
                if (consumeRankModels?.count ?? 0) > 2{
                    return (consumeRankModels?.count ?? 0) - 3 + 1
                }else{
                    return 0
                }
            }else {
                return 0
            }
            
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
       
        let topThreeCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWHomeTopThreeRankCell", for: indexPath) as! HWHomeTopThreeRankCell
        let otherThreeCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWHomeOtherRankCell", for: indexPath) as! HWHomeOtherRankCell
        if indexPath.section == 0 {
            topThreeCell.indexPath = indexPath
            if type == .popularityRank {
                topThreeCell.popularityListModel = rankListModels?[indexPath.row]
            }else if type == .consumeRank {
                topThreeCell.consumeRankModel = consumeRankModels?[indexPath.row]
            }
            
            return topThreeCell
        } else {
            if type == .popularityRank {
                if indexPath.row == 0 {
                    otherThreeCell.rankLabel.text = "排名"
                    otherThreeCell.userLabel.text = "游戏名"
                    otherThreeCell.lowUserLabel.text = "活跃人数"
                }else {
                    otherThreeCell.popularityListModel = rankListModels?[indexPath.row + 3 - 1]
                }
            }else if type == .consumeRank {
                if indexPath.row == 0 {
                    otherThreeCell.rankLabel.text = "排名"
                    otherThreeCell.userLabel.text = "游戏名"
                    otherThreeCell.lowUserLabel.text = "充值"
                }else {
                    otherThreeCell.consumeRankModel = consumeRankModels?[indexPath.row + 3 - 1]
                }
            }
            
            otherThreeCell.indexPath = indexPath
            return otherThreeCell
        }
    }
    
    
}
